package com.yx.shop.dao.impl;

import com.yx.shop.dao.OrderDao;
import com.yx.shop.entitly.Address;
import com.yx.shop.entitly.Item;
import com.yx.shop.entitly.Orders;
import com.yx.shop.entitly.Product;
import com.yx.shop.utils.JDBCUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Classname OrderDaoImpl
 * @Description TODO:
 * @Date 2021/05/09 14:01
 * @Created by yx
 */
public class OrderDaoImpl implements OrderDao {
    private QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());

    @Override
    public Integer createOrder(Orders orders) {
        try {
            String sql = "insert into orders values(?,?,?,?,?,?);";
            Object[] args = {orders.getOid(), orders.getUid(), orders.getAid(), orders.getOcount(), orders.getOtime(), orders.getOstate()};
            return queryRunner.update(sql, args);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Orders> getOrderListByUid(int uid) {
        //联表查询,以获取商品信息与地址信息
        try {
            String sql = "SELECT oid ,orders.aid AS aid ,ocount,ostate,otime,orders.uid AS uid,adetail ,aname ,aphone,astate FROM orders INNER JOIN address ON orders.aid=address.aid WHERE orders.uid= ?;";
            List<Map<String, Object>> query = queryRunner.query(sql, new MapListHandler(), uid);
            if (query != null) {
                //保存数据
                List<Orders> ordersList = new ArrayList<>();
                for (Map<String, Object> stringObjectMap : query) {
                    Orders orders = new Orders();
                    Address address = new Address();
                    //封装数据
                    BeanUtils.populate(orders, stringObjectMap);
                    BeanUtils.populate(address, stringObjectMap);
                    orders.setAddress(address);
                    ordersList.add(orders);
                }
                return ordersList;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Orders selectOrderByOid(String oid) {
        try {
            String sql = "SELECT oid ,orders.aid AS aid ,ocount,ostate,otime,orders.uid AS uid,adetail ,aname ,aphone,astate FROM orders INNER JOIN address ON orders.aid=address.aid WHERE orders.oid= ?;";
            Map<String, Object> query = queryRunner.query(sql, new MapHandler(), oid);
            if (query != null) {
                //保存数据
                Orders orders = new Orders();
                Address address = new Address();
                //封装数据
                BeanUtils.populate(orders, query);
                BeanUtils.populate(address, query);
                orders.setAddress(address);
                return orders;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Item> selectItemListByOid(String oid) {
        try {
            String sql = "SELECT iid,oid,item.pid AS pid,icount,inum,tid,pname,ptime,pimage,pprice,pstate,pinfo FROM item INNER JOIN product ON item.pid = product.pid WHERE oid= ?;";
            List<Map<String, Object>> query = queryRunner.query(sql, new MapListHandler(), oid);
            if (query != null) {
                List<Item> itemListList = new ArrayList<>();
                for (Map<String, Object> stringObjectMap : query) {
                    Product product = new Product();
                    Item item = new Item();
                    //封装数据
                    BeanUtils.populate(product, stringObjectMap);
                    BeanUtils.populate(item, stringObjectMap);
                    item.setProduct(product);
                    itemListList.add(item);
                }
                return itemListList;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer updateStateByOid(String oid, Integer state) {
        try {
            String sql = "UPDATE orders SET ostate = ? WHERE oid = ?;";
            return queryRunner.update(sql,state,oid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
